//index.js


//获取应用实例
const
 app = getApp(),
 api = new (require('../../utils/api.js')),

util = new (require('../../utils/util.js')),
imghead = 'https://techcherry.cn/hhlgsrc/'
let that
Page({
  data: {
    imghead,
    shareid:0,
    name:'',
    number:'',
    agree:!1,
    shopinfo:null,
    haslogin:!1,
    ismember:!1,
    ismemberpage:!1,
    showticketinfo:!1,
    showqx:!1,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  to: function (e) {
    let path = e.currentTarget.dataset.path,url =`../${path}/${path}`
    if(path=='read1'){
      url = '../read/read?q=1'
    }
    wx.navigateTo({  url})
  },
  go(){
    
    let {agree}= that.data
    if(agree){
      wx.setStorageSync('agree', !0)
      wx.navigateTo({
        url: '../road/road',
      })
    }else{
      util.nocancelmodal('请先勾选隐私条款')
    }
  },

  scangift(){
    if(that.data.permission==0){
      util.nocancelmodal('暂无权限')
    }else{
      wx.scanCode({
        onlyFromCamera: true,
        success (res) {
          console.log('scanticket',res);
          if(res.result.indexOf('https://techcherry.cn/giftqrcode?id=')>-1){
            let id= res.result.replace('https://techcherry.cn/giftqrcode?id=','');
          //  console.log('有效扫码'+id);
        //  id=1
          wx.showLoading({
            title: '请稍后',
          })
            api.礼品核销(id,res=>{
              wx.hideLoading()
              if(res.data.issuccess==1){
                util.nocancelmodal("核销成功")
              //  let{ticket} = res.data,{tsrc,price}=ticket
               // that.setData({showticketinfo:!0,ticketsrc:tsrc,ticketprice:price,ticketid:id})
               // that.setData({showdakasucc:!0,count:that.data.count+1})
              }else{
               util.nocancelmodal(res.data.msg)
              }
            })
            
            //加积分
            //加脚印
  
            // that.cleantoast()
            // that.setData({showdakasucc:!0,count:that.data.count+1})
          }else{
            that.cleantoast()
            wx.showToast({
              title: "无效的二维码",
              icon:'none'
            })
          }
        }
      })

    }
  },


  scanticket(){
    if(that.data.permission==0){
      util.nocancelmodal('暂无权限')
    }else{
      wx.scanCode({
        onlyFromCamera: true,
        success (res) {
          console.log('scanticket',res);
          if(res.result.indexOf('https://techcherry.cn/tqrcode?id=')>-1){
            let id= res.result.replace('https://techcherry.cn/tqrcode?id=','');
            console.log('有效扫码'+id);
            api.查看小票(id,res=>{
              if(res.data.issuccess==1){
                console.log(res,'查看小票'); 
                let{ticket} = res.data,{tsrc,price}=ticket
                that.setData({showticketinfo:!0,ticketsrc:tsrc,ticketprice:price,ticketid:id})
               // that.setData({showdakasucc:!0,count:that.data.count+1})
              }else{
               util.nocancelmodal('小票已核销')
              }
            })
            
            //加积分
            //加脚印
  
            // that.cleantoast()
            // that.setData({showdakasucc:!0,count:that.data.count+1})
          }else{
            that.cleantoast()
            wx.showToast({
              title: "无效的二维码",
              icon:'none'
            })
          }
        }
      })

    }
  },

  onLoad: function (opt) {
    var count = 0;

  



    that=this
    let agree =  wx.getStorageSync('agree')
    if(agree) that.setData({agree})
   



  //  wx.getSystemInfo({
  //         success: (res) => {
  //     util.nocancelmodal('宽度:'+res.screenWidth+'高度'+res.screenHeight)
  //          console.log(res,'getSystemInfo');
  //         },
  //   complete:res=>{
  //     console.log(res,'complete');
  //   }
  //       })
    wx.showLoading({
      title: '加载中',
    })
    if(opt.q){
      if(opt.q.indexOf('techcherry.cn')>-1&&opt.q.indexOf('member')>-1){
        wx.hideLoading()
        that.setData({ismemberpage:!0})
       }
      let url = decodeURIComponent(opt.q);
      console.log(url.indexOf('https://techcherry.cn/hhlgqrcode?id=')>-1,'shareidshareid>>>');
      if(url.indexOf('https://techcherry.cn/hhlgqrcode?id=')>-1){
        let shareid= url.replace('https://techcherry.cn/hhlgqrcode?id=','');   
        console.log(shareid,'shareidshareid');
        that.setData({shareid},()=>{
          if (app.logininfo) {
            that.getdata()
          } else {
              util.setwatcher(app, this.afterlogin)
           }
        })
      } 
    }else{
      if (app.logininfo) {
        that.getdata()
      } else {
          util.setwatcher(app, this.afterlogin)
       }
     
    }
   
   
  },
  afterlogin: {
    logininfo: res => {
      that.getdata()
    } 
  },

  input(e){
    let {code} = e.currentTarget.dataset,{value}=e.detail
    console.log(e,'input---e');
    that.setData({[code]:value})
  },
confirm(){
  let{name,number} = that.data,err=''
  if(name.length==0||number.length==0){
    err = '名称或工号输入有误'
  }else{
    //绑定操作
    wx.showLoading({
      title: '绑定中',
    })
    api.绑定(name,number,res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        util.nocancelmodal('绑定成功')
        that.setData({permission:1,shopinfo:{shopname:name,shopnum:number}})
        that.setData({showqx:!1})
      }else{
        util.nocancelmodal('输入错误，请联系管理员')
      }
    })
  }
  if(err!=''){
    util.nocancelmodal(err)
  }
},

toystk(){
  wx.navigateTo({
    url: '../ystk/ystk',
  })
},
oncbchange(e){
  that.setData({agree:e.detail.value[0]=='c'})
},
close(){
  that.setData({showqx:!1})
},
closeticket(){
  that.setData({showticketinfo:!1})
},
checkticket(){
  wx.showLoading({
    title: '提交中',
  })
api.审核小票(that.data.ticketid,res=>{
  that.setData({showticketinfo:!1})
  wx.hideLoading()
  if(res.data.issuccess==1){
    util.nocancelmodal('核销成功')
   
  }else{
    util.nocancelmodal(res.data.msg)
  }
})
},

  qx(){
    that.setData({showqx:!0}) 
  },

  getdata(){

   

wx.hideLoading()
let {logininfo} = app,{user,shopinfo,buttonshare,qrshare}=logininfo,{shareid}=that.data
if(user.permission>0){
  that.setData({ismemberpage:!0,haslogin:!0,shopinfo})
}
if(shareid>0&&qrshare<5){
  api.分享({type:'QR',uid:shareid},res=>{

  })
}
that.setData({ permission:user.permission,myid:user.id})

  },
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
  getUserProfile(res) {
    let that=this
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo, 'resres');
        let userinfo = res.userInfo
        if (res.userInfo) {
          app.globalData.userInfo = userinfo
          that.setData({
            userInfo: userinfo,
            hasUserInfo: true
          })

        } else {
          console.log("点击了拒绝授权");

        }
      },
      fail(res) {
        console.log("点击了拒绝授权");

      },
    })
  },


})
